package framework;

import java.io.File;

import javax.naming.NamingException;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class BrowserFactory {
	public enum BrowserType {
		CHROME("googleChrome"), FIREFOX("firefox"), INTERNET_EXPLORER("iexplore");

		String	val;

		BrowserType(String name) {
			val = name;
		}

		public String toString() {
			return val;
		}
	}

	public static RemoteWebDriver setUp(BrowserType type) {
		RemoteWebDriver driver = null;
		switch (type) {
			case CHROME:
				System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\chromedriver.exe");
				try {
					driver = new ChromeDriver();
				} catch (IllegalStateException e) {
					System.setProperty("webdriver.chrome.driver", "..\\src\\test\\resources\\chromedriver.exe");
					driver = new ChromeDriver();
				}
				break;
			case FIREFOX:
				/* 
				 * Настройки для запуска с прокси
				 * profile.setPreference("network.proxy.type", 1);
				 * profile.setPreference("network.proxy.http", "200.113.15.74");
				 * profile.setPreference("network.proxy.http_port", 3128);
				 * */
				driver = new FirefoxDriver();
				break;
			case INTERNET_EXPLORER:
				File file = new File("F:\\INSTALL\\DEV\\IEDriverServer.exe");
				System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
				DesiredCapabilities cp = DesiredCapabilities.internetExplorer();
				cp.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
				driver = new InternetExplorerDriver(cp);
				break;
		}
		return driver;
	}

	public static RemoteWebDriver setUp(String type) throws NamingException {
		for (BrowserType t : BrowserType.values()) {
			if (t.toString().equalsIgnoreCase(type)) {
				return setUp(t);
			}
		}
		throw new NamingException("browser name is wrong. Acceptable names:\ngoogleChrome\nfirefox\niexplore");
	}

}
